package com.yunchuang.dao.impl;

import com.yunchuang.dao.UserDao;
import com.yunchuang.model.dto.UserUpdateDto;
import com.yunchuang.model.dto.UserUploadAvatarDto;
import com.yunchuang.model.pojo.User;
import com.yunchuang.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;

/**
 * 用户数据层实现类
 *
 * @author ItJiange
 * @date 2023/10/21
 */
public class UserDaoImpl implements UserDao {
    public static final Logger logger = LoggerFactory.getLogger("logback");
    private static final QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());

    /**
     * 根据用户名查询数据
     *
     * @param username
     */
    @Override
    public User selectByUserId(String username) {
        // 定义 sql
        String sql = "select * from user where username = ?;";
        // 执行查询
        try {
            return queryRunner.query(sql, new BeanHandler<>(User.class), username);
        } catch (SQLException e) {
            e.printStackTrace();
            logger.info("sql 执行错误");
            return null;
        }
    }

    /**
     * 根据用户 id 查询
     *
     * @param userId
     * @return
     */
    @Override
    public User selectByUserId(int userId) {
        // 定义 sql
        String sql = "select * from user where id = ?;";
        // 执行查询
        try {
            return queryRunner.query(sql, new BeanHandler<>(User.class), userId);
        } catch (SQLException e) {
            e.printStackTrace();
            logger.info("sql 执行错误");
            return null;
        }
    }

    /**
     * 根据用户名和密码查询数据
     *
     * @param username
     * @param userPassword
     * @return
     */
    @Override
    public User getByNameAndPassword(String username, String userPassword) {
        // 定义 sql
        String sql = "select * from user where userAccount = ? and password = ?;";
        // 执行查询
        try {
            return queryRunner.query(sql, new BeanHandler<>(User.class), username, userPassword);
        } catch (SQLException e) {
            e.printStackTrace();
            logger.info("sql 执行错误");
            return null;
        }
    }

    /**
     * 插入用户数据
     *
     * @param username
     * @param userPassword
     * @param email
     * @return
     */
    @Override
    public int insertUser(String username, String userPassword, String email) {
        // 定义 sql
        String sql = "insert into user (userAccount, password, email) values (?, ?, ?);";
        try {
            User insert = queryRunner.insert(sql, new BeanHandler<>(User.class), username, userPassword, email);
            return insert != null ? 1 : -1;
        } catch (SQLException e) {
            e.printStackTrace();
            logger.info("sql 执行错误");
            return -1;
        }
    }

    /**
     * 修改用户信息
     *
     * @param userUpdateDto
     * @return
     */
    @Override
    public int updateUser(UserUpdateDto userUpdateDto) {
        // 获取修改信息
        Integer userId = userUpdateDto.getUserId();
        String username = userUpdateDto.getUsername();
//        String imageUrl = userUpdateDto.getImageUrl();
        String password = userUpdateDto.getPassword();
        String userEmail = userUpdateDto.getUserEmail();
        String phone = userUpdateDto.getPhone();
        // 定义 sql
        String sql = "update user set userAccount = ?, password = ?, email = ?, phone = ? where id = ?;";
        try {
            return queryRunner.update(sql, username, password, userEmail, phone, userId);
        } catch (SQLException e) {
            e.printStackTrace();
            logger.info("sql 执行错误");
            return -1;
        }
    }

    /**
     * 修改用户头像
     *
     * @param userUploadAvatarDto
     * @return
     */
    @Override
    public int updateUserAvatar(UserUploadAvatarDto userUploadAvatarDto) {
        int userId = userUploadAvatarDto.getId();
        String avatarUrl = userUploadAvatarDto.getAvatarUrl();
        // 定义 sql
        String sql = "update user set avatar = ? where id = ?;";
        try {
            return queryRunner.update(sql, avatarUrl, userId);
        } catch (SQLException e) {
            e.printStackTrace();
            logger.info("sql 执行错误");
            return -1;
        }
    }
}
